﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyDatVe.DataAccessLayer;
using System.Data.SqlClient;
using System.Data;
using QLNHang.DTO;

namespace QLNHang.DAO
{
    class KhachHangDAO
    {

        private ConnectDatabase cn = new ConnectDatabase();
        DBNull dbNULL = DBNull.Value;
        public int ThemKhachHang(KhachHangDTO khDTO)
        {
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_ThemKhachHang", cn.SqlConnect);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@hoTenKhachHang", string.IsNullOrEmpty(khDTO.HoTenKhachHang) ? dbNULL : (object)khDTO.HoTenKhachHang);
            cmd.Parameters.AddWithValue("@diaChi", string.IsNullOrEmpty(khDTO.DiaChi) ? dbNULL : (object)khDTO.DiaChi);
            cmd.Parameters.AddWithValue("@soDienThoai", string.IsNullOrEmpty(khDTO.SoDienThoai) ? dbNULL : (object)khDTO.SoDienThoai);
            cmd.Parameters.AddWithValue("@soCMND", string.IsNullOrEmpty(khDTO.SoCMND) ? dbNULL : (object)khDTO.SoCMND);
           
            int success = cmd.ExecuteNonQuery();
            cn.CloseDatabase();
            return success;
        }
        public int SuaKhachHang(KhachHangDTO khDTO)
        {
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_SuaKhachHang", cn.SqlConnect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@maKhachHang", string.IsNullOrEmpty(khDTO.MaKhachHang) ? dbNULL : (object)khDTO.MaKhachHang);
            cmd.Parameters.AddWithValue("@hoTenKhachHang", string.IsNullOrEmpty(khDTO.HoTenKhachHang) ? dbNULL : (object)khDTO.HoTenKhachHang);
            cmd.Parameters.AddWithValue("@diaChi", string.IsNullOrEmpty(khDTO.DiaChi) ? dbNULL : (object)khDTO.DiaChi);
            cmd.Parameters.AddWithValue("@soDienThoai", string.IsNullOrEmpty(khDTO.SoDienThoai) ? dbNULL : (object)khDTO.SoDienThoai);
            cmd.Parameters.AddWithValue("@soCMND", string.IsNullOrEmpty(khDTO.SoCMND) ? dbNULL : (object)khDTO.SoCMND);
           
            int success = cmd.ExecuteNonQuery();
            cn.CloseDatabase();
            return success;
        }
        public DataTable XemKhachHang(KhachHangDTO khDTO)
        {
            var dt = new DataTable();
            cn.OpenDatabase();
            SqlCommand cmd = new SqlCommand("sp_XemKhachHang", cn.SqlConnect);
            cmd.Parameters.AddWithValue("@maKhachHang", string.IsNullOrEmpty(khDTO.MaKhachHang) ? dbNULL : (object)khDTO.MaKhachHang);
            cmd.Parameters.AddWithValue("@hoTenKhachHang", string.IsNullOrEmpty(khDTO.HoTenKhachHang) ? dbNULL : (object)khDTO.HoTenKhachHang);
            cmd.Parameters.AddWithValue("@diaChi", string.IsNullOrEmpty(khDTO.DiaChi) ? dbNULL : (object)khDTO.DiaChi);
            cmd.Parameters.AddWithValue("@soDienThoai", string.IsNullOrEmpty(khDTO.SoDienThoai) ? dbNULL : (object)khDTO.SoDienThoai);
            cmd.Parameters.AddWithValue("@soCMND", string.IsNullOrEmpty(khDTO.SoCMND) ? dbNULL : (object)khDTO.SoCMND);

            var sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            cn.CloseDatabase();
            return dt;
        }
    }
}
